
package com.rt.appauthorize;

import org.slf4j.LoggerFactory;
import org.springframework.boot.system.ApplicationHome;

import java.io.File;

import org.slf4j.Logger;

public class AppAuthPathUtil {
    private static Logger log;

    public static String dirPathFormate(String dirPath) {
        dirPath = dirPath.replace("\\", "/");
        dirPath = (dirPath.endsWith("/") ? dirPath : (dirPath + "/"));
        return dirPath;
    }

    public static String filePathFormate(String filePath) {
        filePath = filePath.replace("\\", "/");
        return filePath;
    }

    public static String getFileNameByFilePath(String filePath) {
        filePath = filePathFormate(filePath);
        if (filePath.endsWith("/")) {
            AppAuthPathUtil.log.error("路径不正确，路径：{}", (Object) filePath);
            return null;
        }
        String[] arry = filePath.split("/");
        int len = arry.length;
        if (len > 0) {
            return arry[len - 1];
        }
        AppAuthPathUtil.log.error("路径不正确，路径：{}", (Object) filePath);
        return null;
    }

    public static String getFileDirByFilePath(String filePath) {
        File file = new File(filePath);
        String parentDir = file.getParentFile().getAbsolutePath();
        parentDir = filePathFormate(parentDir);
        return parentDir;
    }

    public static String getRemotePathByLocalPath(String tempDir, String localPath) {
        String remoteFilePath = localPath.replace(tempDir, "");
        if (remoteFilePath.startsWith("/")) {
            remoteFilePath = remoteFilePath.replaceFirst("/", "");
        }
        return remoteFilePath;
    }

    public static String getAppJarPath() {
        ApplicationHome home = new ApplicationHome(AppAuthPathUtil.class);
        File homeFile = home.getDir();
        String jarDir = dirPathFormate(homeFile.getAbsolutePath());
        return jarDir;
    }

    static {
        log = LoggerFactory.getLogger(AppAuthPathUtil.class);
    }
}
